package qcurepay

import (
    "fmt"
	"github.com/astaxie/beego"
	"thirdpaysdk/utils/mapstruct"
)

type RefundResponse struct {
	RespCode         string  `json:"respCode"`
	RespMsg          string  `json:"respMsg"`
	Version          string  `json:"version"`
	OrgCode          string  `json:"orgCode"`
	MerchCode        string  `json:"merchCode"`
	PayType          string  `json:"payType"`
	MerchOrderNo     string  `json:"merchOrderNo"`
	OrderNo          string  `json:"orderNo"`
	OriginalRespCode string  `json:"originalRespCode"`
	OriginalRespMsg  string  `json:"originalRespMsg"`
	Sign             string  `json:"sign"`
}

func  Refunds(orgCode,merchCode,appsecret,orderId,refundOrderId,attach string,amount int)(rsp *RefundResponse, err error){
	reqURL := "http://www.qcurepay.com/trade/route"
	type RefundReq struct {
		Version       string `json:"version"`
		OrgCode       string `json:"orgCode"`
		MerchCode     string `json:"merchCode"`
		RefundOrderNO string `json:"refundOrderNO"`    
		MerchOrderNo  string `json:"merchOrderNo"`
		Amount        int    `json:"amount"`
		PayType       string `json:"payType"`
		Remark        string `json:"remark"`
	}
	req := new(RefundReq)
	req.Version = "V1.0"
	req.OrgCode = orgCode
	req.MerchCode = merchCode
	req.RefundOrderNO = orderId
	req.MerchOrderNo = refundOrderId
	req.Amount = amount
	req.PayType = "wxgjpay_refund"
	req.Remark = attach
	//
	sdk := NewSDK(reqURL, appsecret)
	resp, err := sdk.Request(req)
	if err != nil {
	   beego.Debug("err:", err.Error())
	   return nil, err
	}
	if resp["respCode"] != "00"{
	   beego.Debug("error:", fmt.Sprintf("respCode=%s,respMsg=%s",resp["respCode"],resp["respMsg"]))
	   return nil, err
	}
	//beego.Debug("respMap", respMap)
	rsp = new(RefundResponse)
	err = mapstruct.Map2Struct(resp, rsp, "json")
	return
}
